package.cpath = "luaclib/?.so"
package.path = "lualib/?.lua;server/src/common/?.lua;service/?.lua"

if _VERSION ~= "Lua 5.3" then
	error "Use lua 5.3"
end


local socket = require "client.socket"
local proto = require "proto.game"
local sproto = require "sproto"


proto.init("./server/conf/proto.conf")
proto.parse()

local host = sproto.new(proto.s2c):host "package"
local request = host:attach(sproto.new(proto.c2s))

local login_fd = assert(socket.connect("192.168.190.138", 6090))
local char_fd
local auth
local login_flag
local PASS 

local function send_package(fd, pack)
	local package = string.pack(">s2", pack)
	socket.send(fd, package)
end

local function unpack_package(text)
	local size = #text
	if size < 2 then
		return nil, text
	end
	local s = text:byte(1) * 256 + text:byte(2)
	if size < s+2 then
		return nil, text
	end

	return text:sub(3,2+s), text:sub(3+s)
end


local session = 0

local function send_request(fd, name, args)
	session = session + 1
	local str = request(name, args, session)
	send_package(fd, str)
	
end

local last = ""

local function print_request(name, args)
	
	if args then
		for k,v in pairs(args) do

			if type(v) == "table" then 
				print_request("", v)
			end 
			print(k,v)
		end
	end
end


local function print_response(session, args)
	
	if args then

		for k,v in pairs(args) do

			if type(v) == "table" then 
				print_response("", v)
			end 

			print(k,v)
		end
	end
end

local function print_package(t, ...)
	if t == "REQUEST" then
		print_request(...)
	else
		assert(t == "RESPONSE")
		print_response(...)
	end
end


local function input()

		local str = ''
		
		while true do 

			str = socket.readstdin()

			if str and str ~= ""  or str == "quit" then
				break 
			end 
		end 
	
		return str
end 


local function recv_pack(fd)

	local data
		while true do 

			data = socket.recv(fd)

			if data ~= nil  then 
				break 
			end 
		end 

	local v,resutl = unpack_package(data)

	if v then 
		return v
	end 
	
	--print_package(host:dispatch(v))
end 


local function login()

	local name,pass 
	
	print("请输入用户名:")
	name = input()
	print("请输入密  码:")
	pass  = input()
	
	return {user = name, pawd = pass}
end 

local function register()


	local name,pass 
	
	print("请输入用户名:")
	name = input()
	print("请输入密码:")
	pass  = input()

	return {user = name, pawd = pass}	
end 

local function option()

	local opt

	print("请输入选择项： 1.登录  2.注册 3.退出")

	opt = input()

	if opt == "1" then 

			local ret = login()
			send_request(login_fd,"login",ret)

			local result = recv_pack(login_fd)
			local type,name,arg = host:dispatch(result)

			if arg.result == 0 and arg.session ~= 0  then 
				
				auth = arg	
				login_flag = true
				PASS = ret.pawd
				print("登录成功 session",arg.session,arg.result)

				return 1
			else 
				
				login_flag = false
				print("登录失败 result = ", arg.result)
				return 2
			end 

	elseif opt == "2" then 

		local ret = register()
			print("注册账号:" .. ret.user .. "密码: " .. ret.pawd)
			send_request(login_fd,"register", ret)
			local result = recv_pack(login_fd)			
			print_package(host:dispatch(result))
			return 2
			
	elseif opt == "3" then 

		send_request(login_fd,"quit")
		return 3
	else

	end 	
end 


-- 角色服务器
local function char_connect()

	if not char_fd then 
		char_fd = assert(socket.connect("192.168.190.138", 6091))
	end  
end 

local function char_pick(sen)

		print("发送的角色数据是:", sen.account_id, sen.char_id, sen.name, sen.slot)

		send_request(char_fd, "char_pick", { pick = {account_id = sen.account_id, char_id = sen.char_id, name = sen.name, slot = sen.slot}})	
		
		local result = recv_pack(char_fd)
		local type,name,arg = host:dispatch(result)
			
	if arg then 
		print_response("", arg)
	end
end 


local function char_list()

	send_request(char_fd, "char_list")
	local result = recv_pack(char_fd)
	local type,name,arg = host:dispatch(result)
	
	if arg then 

		print_response("", arg.list[1])
		char_pick(arg.list[1])
	end 

end 


local function main()



	while true do 

		local ret = option()
		if ret == 3 then 
			return 
		end 

		if ret == 1 then 
		
			char_connect()
			send_request(char_fd, "auth", {session = auth.session,token = PASS})
			print("发送验证回话 和密码",auth.session,PASS)
			local result = recv_pack(char_fd)
			local type,name,arg = host:dispatch(result)

			if arg.result and arg.result == 0 then 
				char_list()
			else 
				print("验证失败")
			end 

		end 

		

		

	end 
end 


main()



